package com.feng.cloud.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.feng.cloud.common.annotation.ControllerEndpoint;
import com.feng.cloud.common.exception.system.SystemStatus;
import com.feng.cloud.common.request.QueryRequest;
import com.feng.cloud.common.response.RestResponse;
import com.feng.cloud.common.response.TableResultResponse;
import com.feng.cloud.system.entity.Role;
import com.feng.cloud.system.service.RoleService;
import com.wuwenze.poi.ExcelKit;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * @ClassName RoleController
 * @Description TODO
 * @Author Better Feng
 * @Date 2019/12/26 11:29
 * @Version 1.0
 **/
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
public class RoleController implements IRoleController {

    private final RoleService roleService;

    /**
     * 删除角色
     * @param roleIds roleIds
     * @return RestResponse<Void>
     */
    @Override
    public RestResponse<Void> deleteRoles(@NotBlank(message = "{required}") String roleIds) {
        roleService.deleteRoles(roleIds.split(StringPool.COMMA));
        return new RestResponse<>();
    }

    /**
     *  更新角色
     * @param role Role
     * @return RestResponse<Role>
     */
    @Override
    @PreAuthorize("hasAuthority('role:update')")
    @ControllerEndpoint(operation = "修改角色", exceptionMessage = "修改角色失败")
    public RestResponse<Role> updateRoleById(@Valid Role role) {
        roleService.updateRoleById(role);
        return new RestResponse<Role>().data(role);
    }

    /**
     * 新增角色
     * @param role Role
     * @return RestResponse<Role>
     */
    @Override
    public RestResponse<Role> add(@Valid Role role) {
        roleService.addRoleAndMenus(role);
        return new RestResponse<Role>().data(role);
    }

    /**
     * 根据角色名查询角色
     * @param roleName roleName
     * @return RestResponse<Role>
     */
    @Override
    @PreAuthorize("hasAuthority('role:view')")
    public RestResponse<Role> selectRoleByName(String roleName) {
        Role role = roleService.selectRoleByName(roleName);
        if (role != null) {
            return new RestResponse<Role>().data(role);
        }
        return new RestResponse<>(SystemStatus.ROLE_NOT_EXIST);
    }

    /**
     * 分页查询
     * @param params Map
     * @return TableResultResponse<Role>
     */
    @Override
    @PreAuthorize("hasAuthority('role:view')")
    public TableResultResponse<Role> selectPage(@RequestParam Map<String, Object> params) {
        QueryRequest request = new QueryRequest(params);
        IPage<Role> iPage = roleService.selectListByPage(request);
        return new TableResultResponse<>(iPage.getTotal(), iPage.getRecords());
    }

    @Override
    public RestResponse<List<Role>> selectList() {
        return new RestResponse<List<Role>>().data(roleService.selectList());
    }

    @PostMapping("excel")
    @PreAuthorize("hasAuthority('role:export')")
    @ControllerEndpoint(operation = "导出角色数据", exceptionMessage = "导出Excel失败")
    public void export(@RequestParam Map<String,Object> params, HttpServletResponse response) {
        QueryRequest request = new QueryRequest(params);
        List<Role> roles = this.roleService.selectListByPage(request).getRecords();
        ExcelKit.$Export(Role.class, response).downXlsx(roles, false);
    }
}
